﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Data;
using System.Globalization;


public partial class UserControl_ucWorkDetail : System.Web.UI.UserControl
{
    TblFaultListDAO FaultListDAO = new TblFaultListDAO(Global.CONNECTION_STRING);
    TblStatusJobDAO StatusJobDAO = new TblStatusJobDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //LoadLastStatus();
            LoadFaultList();

            string id = Request.QueryString["id"];
            if (id != null )
            {
                btnMarkJobcomplete.Enabled = true;
                btnmarkjob.Enabled = true;
            }
            else
            {
                btnMarkJobcomplete.Enabled = false;
                btnmarkjob.Enabled = false;
            }
        }

    }

    private void LoadLastStatus()
    {
        string id = Request.QueryString["id"];
        DataTable LastStusdt = StatusJobDAO.LoadLastStatus(Convert.ToInt32(id));
        foreach (DataRow dr in LastStusdt.Rows)
        {
            txtdetail.Text = dr["Notes"].ToString();
        }
    }
    protected void btnmarkjob_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblJobStatus JobStatus = new TblJobStatus();
            JobStatus.JobID = Convert.ToInt32(id);
            JobStatus.StatusDate = Convert.ToDateTime(DateTime.Now);
            JobStatus.StatusJobs = "BER";
            JobStatus.Notes = "BER - Beyond Economical Repair " + txtdetail.Text;
            StatusJobDAO.JobStatusIns(JobStatus);

            StatusJobDAO.UpdateQuoteComplete(JobStatus.JobID, JobStatus.StatusJobs);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Booking.aspx?id=" + id);
        }
    }
    protected void btnMarkJobcomplete_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string id = Request.QueryString["id"];
        try
        {
            
            TblJobStatus JobStatus = new TblJobStatus();
            JobStatus.JobID = Convert.ToInt32(id);
            JobStatus.StatusDate = Convert.ToDateTime(DateTime.Now);
            JobStatus.StatusJobs = "Repair Completed";
            JobStatus.Notes = txtdetail.Text;
            StatusJobDAO.JobStatusIns(JobStatus);

            StatusJobDAO.UpdateQuoteComplete(JobStatus.JobID, JobStatus.StatusJobs);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
            Response.Redirect("~/Admin/Booking.aspx?id=" + id);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        txtdetail.Text += listboxWorkDetail.SelectedItem.Text + "; ";

    }

    private void LoadFaultList()
    {
        //listboxWorkDetail.DataSource = FaultListDAO.GetFaultList();
        //listboxWorkDetail.DataTextField = "NameFault";
        //listboxWorkDetail.DataValueField = "FaultID";
        //listboxWorkDetail.DataBind();
        listboxWorkDetail.Items.Add("Beyound Economical Repair");
        listboxWorkDetail.Items.Add("Replace LCD, tested OK");
        listboxWorkDetail.Items.Add("Replace touchscreen, tested OK");
        listboxWorkDetail.Items.Add("Repair and tested OK");
        listboxWorkDetail.Items.Add("BER");
    }
}
